#define _CRT_SECURE_NO_WARNINGS 2

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void CountSort(int* a, int n) {
	int max = a[0], min = a[0];
	for (int i = 0; i < n; i++) {
		if (a[i] >max ) {
			max = a[i];
		}
		if (a[i] < min) {
			min = a[i];
		}
	}
	int len = max - min+1;
	int* countA = (int*)malloc(sizeof(int) *len);
	if (countA == NULL) {
		perror("fail malloc");
		return;
	}
	memset(countA, 0, sizeof(int) * len);
	for (int i = 0; i < n; i++) {
		countA[a[i] - min]++;
	}
	int j = 0;
	for (int i = 0; i < len; i++) {
		while (countA[i]--) {
			a[j++] = i + min;
		}
	}
}

int main() {
	int arr[] = { 1,2,4,4,9,8,6,6,20,-1,-4,-11};
	int len = sizeof(arr) / sizeof(arr[0]);
	CountSort(arr, len);
	for (int i = 0; i < len; i++) {
		printf("%d ", arr[i]);
	}
	return 0;
}